package com.example.demo.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.pojo.Admin;
import com.example.demo.pojo.Comment;
import com.example.demo.pojo.User;
import com.example.demo.service.AdminService;
import com.example.demo.service.CommentService;
import com.example.demo.service.UserService;

@RestController
@RequestMapping(value = "/test")
public class Test {
	
	@Autowired
	AdminService adminService;

	@Autowired
	UserService userService;
	@Autowired
	CommentService commentService;
	
	@RequestMapping("/test4")
	@ResponseBody
	public String test4() {
		System.out.println("zan");
		
		User user = userService.find(1);
		
		int zan = user.getAgree();
		String zanstr = String.valueOf(zan);
		
		return zanstr;
	}
	
	@RequestMapping("/login")
	@ResponseBody
	public String login(@RequestBody Admin admin) {
		
		
		
		Admin admin_database = adminService.find(1);
		
		if (admin_database.getUsername().equals(admin.getUsername())&&admin_database.getPassword().equals(admin.getPassword())) {
			return "success";
		} else {
			return "fail";
		}
		
		
	}
	
	
	@RequestMapping("/test3")
	@ResponseBody
	public String test3() {
		System.out.println("zan");
		
		User user = userService.find(1);
		int zan = user.getAgree();
		
		zan++;
		
		user.setId(1);
		user.setAgree(zan);
		
		userService.modify(user);
		
		
		
		
		return "success";
	}
	
	
	
	@RequestMapping("/getComment")
	public String getComment() {
		List<Comment> list = commentService.findAll();
		String str = JSONArray.toJSONString(list);
		System.out.println(str);
		return str;
	}
	

}
